package com.starlink.model.excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSONObject;
import com.starlink.model.excel.MallBookExcelDTO;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: zhouyuhang01
 * @Date: 2024/6/9 12:15
 * @Description: MallBookListener
 * @Version 1.0.0
 */
public class MallBookListener extends AnalysisEventListener<Object> {
    private List<MallBookExcelDTO> dataList = new ArrayList<>();
    @Override
    public void invoke(Object object, AnalysisContext context) {
        if (!ObjectUtils.isEmpty(object)) {
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(object));
            String mallBooksId = jsonObject.getString("0");
            String freeTemplateName = jsonObject.getString("1");
            String bookName = jsonObject.getString("2");
            String author = jsonObject.getString("3");
            String isbn = jsonObject.getString("4");
            String publishingHouse = jsonObject.getString("5");
            String picture = jsonObject.getString("6");
            MallBookExcelDTO mallBookExcelDTO = new MallBookExcelDTO();
            mallBookExcelDTO.setMallBooksId(mallBooksId);
            mallBookExcelDTO.setFreeTemplateName(freeTemplateName);
            mallBookExcelDTO.setBookName(bookName);
            mallBookExcelDTO.setAuthor(author);
            mallBookExcelDTO.setIsbn(isbn);
            mallBookExcelDTO.setPublishingHouse(publishingHouse);
            dataList.add(mallBookExcelDTO);
        }
    }


    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }



    public List<MallBookExcelDTO> getDataList() {
        return dataList;
    }
}
